package com.lagou.utils;




import com.lagou.anno.Autowired;
import com.lagou.anno.Component;

import java.sql.SQLException;

/**
 * 添加事务管理类
 */
@Component("transactionManager")
public class TransactionManager {

    @Autowired
    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }


    /**
     * 关闭自动提交事务，手动开启
     * @throws SQLException
     */
    public  void beginTransaction() throws SQLException {
        connectionUtils.getConnection().setAutoCommit(false);
    }

    /**
     * 提交事务
     * @throws SQLException
     */
    public void commitTransction() throws SQLException {
        connectionUtils.getConnection().commit();
    }

    /**
     * 回滚事务
     * @throws SQLException
     */
    public void rollbackTransction() throws SQLException {
        connectionUtils.getConnection().rollback();
    }

}
